﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace NicoView.Model.Share.Speech.Learn
{
    public sealed class GeneralLearnModel:ILearnModel
    {
        public GeneralLearnModel()
        {
            LearnDictionary = null;
        }



        public void Learn(string key, string text)
        {
            LearnDictionary.Add(key, new Learnword(key,text) );
        }

        public void Oblivion(string key)
        {
            LearnDictionary.Remove(key);
        }

        public void RegexLearn(string key, string text)
        {
            RegexLearnDictionary.Add(key, new Learnword(key,text));
        }

        public void RegexOblivion(string key)
        {
            RegexLearnDictionary.Remove(key);
        }


        private static readonly Regex _learnRegex = new Regex(@"^教育\((.+)\s*=\s*(.+)\)");

        private static readonly Regex _oblivionRegex = new Regex(@"^忘却\((.+)\)");

        private static readonly Regex _synthesizeRegex = new Regex(@"\w+");


        

        public string Convert(string text)
        {


            text = RegexLearnDictionary.SystemRegexLearnDictionary.Replace(text);

            
            if (RegexLearnDictionary.LearnRegex != null)
            {

                text = RegexLearnDictionary.Replace(text);
                
            }

            

            text = LearnDictionary.Replace(text);


            
            var synthesizeMatches = _synthesizeRegex.Matches(text);
            string rsltText = "";
            foreach (Group synthesizeMatch in synthesizeMatches)
            {
                if (synthesizeMatch.Success)
                {
                    rsltText += synthesizeMatch.Value + " ";
                }
            }

            if (rsltText.Length == 0) return "";
            return rsltText.Remove(rsltText.Length - 1);
        }




        public string CheckTextCommand(string text)
        {
            

            var learnMatch = _learnRegex.Match(text);

            if (learnMatch.Success)
            {
                Learn(learnMatch.Groups[1].Value, learnMatch.Groups[2].Value);

                return learnMatch.Groups[1].Value + "わ" + learnMatch.Groups[2].Value + "を覚えました";
            }

            var oblivionMatch = _oblivionRegex.Match(text);

            if (oblivionMatch.Success)
            {
                Oblivion(oblivionMatch.Groups[1].Value);

                return oblivionMatch.Groups[1].Value + "を忘れました";
            }

            
            return "";
        }

        public RegexLearnDictionary RegexLearnDictionary
        {
            get;
            set;
        }

        public LearnDictionary LearnDictionary
        {
            get;
            set;
        }


        

        
    }
}
